package cn.wolfcode.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/5/6.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    //查询
    //可以配置多个,但默认是与的,我们可以改成或,因为我们需要中文的
    @RequiresPermissions(value = {"employee:list","员工列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")EmployeeQueryObject qo){
        model.addAttribute("pageInfo",employeeService.query(qo));
        model.addAttribute("departments",departmentService.listAll());
        return "employee/list";
    }
    //编辑,跳转到新增或者修改的jsp
    @RequestMapping("/input")
//    @RequiresRoles("admin")贴这个注解表示必须要admin才能访问
    @RequiresPermissions("employee:input")
    public String input(Model model,Long id){
        model.addAttribute("departments",departmentService.listAll());
        //查询所有角色
        model.addAttribute("roles",roleService.listAll());
        if(id != null){
            model.addAttribute("employee",employeeService.get(id));
        }
        return "employee/input";
    }
    //保存修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        try {
            if(employee.getId() != null){
                employeeService.update(employee,ids);
            }else {
                employeeService.save(employee,ids);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }

    }
    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if(id != null){
            employeeService.delete(id);
        }
        return new JsonResult();
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        try {
            employeeService.batchDelete(ids);
            return new JsonResult();
        } catch (Exception e){
            return new JsonResult(false,"删除失败");
        }
    }

    //验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name){  // valid:true
        Employee employee = employeeService.selectByName(name);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",employee==null);//返回true
        return map;
    }
    //验证用户名是否存在
    @RequestMapping("/check")
    @ResponseBody
    public Map<String,Boolean> check(String name,Long id){  // valid:true
        if(id != null){
            Employee employee = employeeService.get(id);
            if(employee.getName()==name){
                HashMap<String, Boolean> map = new HashMap<>();
                map.put("valid",employee==null);
                return map;
            }
        }
            Employee employee = employeeService.selectByName(name);
            if(employee.getName()==name){
                HashMap<String, Boolean> map = new HashMap<>();
                map.put("valid",employee!=null);
                return map;
            }
                HashMap<String, Boolean> map = new HashMap<>();
                map.put("valid",employee==null);
                return map;
    }


    //导出
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //调用方法
//        Workbook workbook = employeeService.exportXls();
        //输出文件
//        workbook.write(response.getOutputStream());
        //使用阿里的easyexcel
        List<Employee> employees = employeeService.listAll();
        EasyExcel.write(response.getOutputStream(), Employee.class).sheet("员工通讯录").doWrite(employees);
    }
    //导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        //读上传过来的文件
//        file.getOriginalFilename();
        employeeService.importXls(file);
        return new JsonResult();
    }

}
